﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace ProjetPlatformer.gameElements
{
    public class ControlableGameElement : DrawableGameElement, IFocusable
    {

        public const int sautDuree = 150;
        int sautTimer = 0;
        protected bool jumping = false;
        protected bool moving = false;
        protected bool protecting = false;
        const int forceSaut = 20;
        const int gravity = 2;
        const int velocityMax = 15;
        public float gravityForce = gravity;
        const float deltaGravityForce = 0.5f;

        public List<GameElement> collidingElements;
        public List<Vector2> correctionVectors;

        public ControlableGameElement(Game game, SpriteBatch sb, ContentManager cm) : base(game,sb,cm)
        {
            position = new Vector2(0, 0);
            velocity = new Vector2(8, 15);
            size = new Vector2(64,64);
            spriteBatch = sb;
            contentManager = cm;
            bBox = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);
            collidingElements = new List<GameElement>();
            image = contentManager.Load<Texture2D>("carre");
            collidingElements = new List<GameElement>();
            correctionVectors = new List<Vector2>();
        }

        public Rectangle getCoin(int i)
        {
            int x = bBox.X;
            int y = bBox.Y;
            int width=bBox.Width;
            int height = bBox.Height;
            switch (i)
            {
                case 0: return new Rectangle(x, y, 1, 1); break ;
                case 1: return new Rectangle(x + width - 1, y, 1, 1); break;
                case 2: return new Rectangle(x + width - 1, y + height - 1, 1, 1); break;
                case 3: return new Rectangle(x, y + width - 1, 1, 1);
                default: return new Rectangle(0,0,0,0);
                    break;
            }
        }

        public override void Initialize()
        {
            base.Initialize();
        }
        


        protected override void LoadContent()
        {
            base.LoadContent();
            //DEBUG
            //Console.WriteLine("controlableElement.loadContent()");
            
        }

        
        public override void Update(GameTime gameTime)
        {
            KeyboardState kbs =  Keyboard.GetState();
            Vector2 nextPosition = new Vector2(position.X, position.Y);
            moving = false;
            protecting = false;
            if (kbs.IsKeyDown(Keys.Up))
            {
                nextPosition.Y -= velocity.Y;
                moving = true;
            }

            if (kbs.IsKeyDown(Keys.Down))
            {
                protecting = true;
            }

            if (kbs.IsKeyDown(Keys.Left))
            {
                nextPosition.X -= velocity.X;
                this.sprite.Flip = true;
                moving = true;
            }
            

            if (kbs.IsKeyDown(Keys.Right))
            {
                nextPosition.X += velocity.X;
                this.sprite.Flip = false;
                moving = true;
            }

            //Console.WriteLine(" onground : " + this.onGround);

            //jumping = vrai le temps du saut en ascencion 
            if (jumping)
            {
                //DEBUG
                //Console.WriteLine("jumping");
                
                //si le temps du saut est fini, on passe jumping a false
                if (sautTimer > sautDuree)
                {
                    jumping = false;
                    sautTimer = 0;
                }
                //tant que le temps du saut n'est pas fini, on applique la force du saut
                else
                {
                    sautTimer += gameTime.ElapsedGameTime.Milliseconds;
                    nextPosition.Y -= forceSaut;
                }

            }

            if (!jumping && this.isOnground)
            {
                gravityForce = gravity;
                //DEBUG
                //Console.WriteLine("onground and not jumping");
                if (kbs.IsKeyDown(Keys.Space))
                {
                    this.isOnground = false;
                    this.jumping = true;
                    sautTimer = 0;
                }

            }
            else
            {
                //DEBUG
                //Console.WriteLine("jumping ou not on ground");
                if (gravityForce < velocityMax)
                {
                    gravityForce+=deltaGravityForce;
                }
            }
            //DEBUG affichage de la gravité à chaque frame
            //Console.WriteLine("gravity Force : " + gravityForce);
            nextPosition.Y += gravityForce;

            

            this.isOnground = false;
            //Vector2 delta = position - nextPosition;
            //Vector2 correction = checkCollision(delta);
            //Vector2 positionCorrige = nextPosition + correction;
            //Console.WriteLine(delta);
            this.setPosition(nextPosition);
        }


        

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
                spriteBatch.Draw(image, position, Color.Aqua);
            
        }



    }
}
